﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLULearningNetworkModel
{
    public partial class FeedComment
    {
        public static int Add(FeedComment _FeedComment)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    db.FeedComments.AddObject(_FeedComment);
                    if (db.SaveChanges() > 0)
                    {
                        return 1;
                    }
                    return 0;
                }
            }
            catch
            {
                return 0;
            }
        }

        public static FeedComment SelectByFeedCommentID(Guid feedCommentID)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    return db.FeedComments.Include("Account")
                        .Include("Feed").Include("Feed.Course")
                        .SingleOrDefault(fc => fc.FeedCommentID_u == feedCommentID);
                }
            }
            catch
            {
                return null;
            }
        }

        public static List<FeedComment> SelectByFeedID(Guid feedID, int numberOfSkip, int numberOfTake)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    return db.FeedComments
                        .Include("Account")
                        .Include("Feed").Include("Feed.Course")
                        .Where(f=>f.FeedID_u==feedID)
                        .OrderByDescending(fc => fc.CreateDate_dt)
                        .Skip(numberOfSkip).Take(numberOfTake)
                        .ToList();
                }
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// Xoa feed comment, truoc khi xoa phai kiem tra nguoi xoa co phai nguoi tao khong
        /// </summary>
        /// <param name="feedCommentID"></param>
        /// <param name="accountID"></param>
        /// <returns>1: thanh cong, 2: khong co quyen xoa, 0: khong tim thay,100: Loi co so du lieu; 101: try catch</returns>
        //public static int DeleteFeedComment(Guid feedCommentID, Guid accountID)
        //{
        //    try
        //    {
        //        using (DLULNEntities db = new DLULNEntities())
        //        {
        //            FeedComment fcmt = db.FeedComments.Include("Feed").SingleOrDefault(fc => fc.FeedCommentID_u == feedCommentID);
        //            if (fcmt != null)
        //            {
        //                if (fcmt.ActiveAccount_u == accountID || fcmt.FeedReference.Value.ActiveAccountID_u == accountID)
        //                {
        //                    db.FeedComments.DeleteObject(fcmt);
        //                    if (db.SaveChanges() > 0)
        //                    {
        //                        return 1;
        //                    }
        //                    else
        //                    {
        //                        return 100;
        //                    }
        //                }
        //                else
        //                {
        //                    return 2;
        //                }
        //            }
        //            else
        //            {
        //                return 0;
        //            }
        //        }
        //    }
        //    catch
        //    {
        //        return 101;   
        //    }
        //}

        public static int Delete(Guid feedCommentID)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    FeedComment feedComment = db.FeedComments.SingleOrDefault(fcmt => fcmt.FeedCommentID_u == feedCommentID);
                    if (feedCommentID != null)
                    {
                        //Truoc khi delete comment, delete feed lien quan comment nay
                        var lstFeedActivity = db.Feeds.Where(f => f.CommentID == feedComment.FeedCommentID_u);
                        foreach (var feed in lstFeedActivity)
                        {
                            db.Feeds.DeleteObject(feed);
                        }
                        db.FeedComments.DeleteObject(feedComment);
                        if (db.SaveChanges() > 0)
                        {
                            return 1;
                        }
                        return 0;
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
            catch
            {
                return 101;
            }       
        }
    }
}
